<?php


namespace App\Http\Controllers\WebApi;


use App\Models\ImSessionInfo;
use Gregwar\Captcha\CaptchaBuilder;
use Gregwar\Captcha\PhraseBuilder;
use Illuminate\Http\Request;

class CaptchaController
{
    public function index(Request $req)
    {
        $phrase = new PhraseBuilder();
        //验证码长度
        $code = $phrase->build(4);

        // 生成验证码图片的Builder对象,配置相应属性
        $builder = new CaptchaBuilder($code, $phrase);

        // 设置背景颜色
        $builder->setBackgroundColor(55, 136, 216);

        // 设置倾斜角度
        $builder->setMaxAngle(25);

        // 设置验证码后面最大行数
        $builder->setMaxBehindLines(10);

        // 设置验证码前面最大行数
        $builder->setMaxFrontLines(10);

        // 设置验证码颜色
        $builder->setTextColor(230, 200, 175);

        // 可以设置图片宽高及字体
        $builder->build($width = 150, $height = 50, $font = null);


        // 获取验证码的内容
        $phrase = $builder->getPhrase();

        // 把内容存入session
        $key = 'webLoginCaptcha:' . $req->ip();
        $session = new ImSessionInfo();
        $session->setCaptChaSession($key, $phrase);

        // 生成图片
        header('Cache-Control: no-cache, must-revalidate');
        header('Content-Type:image/jpeg');
        $builder->output();
    }
}
